package com.tgy.leetcode;

/**
 * https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/
 */
public class _81_搜索旋转排序数组_II_01 {

    public static boolean  search(int[] nums, int target) {


        if (nums == null || nums.length == 0) {

            return false;
        }

        if (nums.length == 1) {

            return nums[0] == target;
        }

        int len = nums.length;
        int left = 0, right = len - 1, mid;

        while (left <= right) {

            mid = left + ((right - left) >> 1);

            if (nums[mid] == target) {

                return true;
            }

            if (nums[left] == nums[mid] && nums[mid] == nums[right]) {

                left++;
                right--;
                continue;
            }

            if (nums[mid] <= nums[right]) {

                if (nums[mid] < target && target <= nums[right]) {

                    left = mid + 1;
                }else {

                    right = mid - 1;
                }

            }else {

                if (target >= nums[left] && target < nums[mid]) {

                    right = mid - 1;
                }else {

                    left = mid + 1;
                }
            }

        }

        return false;
    }

    public static void main(String[] args) {

        // [1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1]
        // 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
//        int[] nums = {2,5,6,0,0,1,2};
        int[] nums = {1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1};
        System.out.println(search(nums, 2));

    }
}
